package com.atguigu.examsystemserver.service.impl;


import com.atguigu.examsystemserver.properties.MinioProperties;
import com.atguigu.examsystemserver.service.FileUploadService;
import io.minio.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * projectName: com.atguigu.exam.service.impl
 *
 * @author: 赵伟风
 * description:
 */

@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    MinioProperties minioProperties;
    @Override
    public String uploadfile(MultipartFile file) {
        try {
            // 1.创建MinioClient对象
            MinioClient minioClient =
                    MinioClient.builder()
                            //设置mimio服务器的地址
                            .endpoint(minioProperties.getEndpoint())
                            //设置密钥对，如果没有生成则使用用户名和密码
                            .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                            .build();

            // 2.判断某个桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (!found) {
                // 如果某个桶不存在则创建它
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            } else {
                System.out.println("Bucket "+minioProperties.getBucketName()+" already exists.");
            }

            /*
                上传本地文件的时候使用UploadObjectArgs对象
                通过输入流上传文件使用PutObjectArgs
             */

            String originalFilename = file.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            //使用UUID随机生成一个字符串作为文件的名字
            String fileNamePrefix = UUID.randomUUID().toString().replaceAll("-", "");
            String format = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
            String newFileName = format + "/" + fileNamePrefix + substring;
            InputStream is = file.getInputStream();
            long size = file.getSize();
            //3.创建UploadObjectArgs对象
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    //指定桶的名字
                    .bucket(minioProperties.getBucketName())
                    //指定上传到minio服务器之后的文件名
                    .object(newFileName)
                    //指定输入流
                    .stream(is,size,-1)
                    .build();



            // 4.上传文件到minio服务器
            minioClient.putObject(putObjectArgs);
            System.out.println("文件上传成功");
            return minioProperties.getEndpoint()+"/"+minioProperties.getBucketName()+"/"+newFileName;
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
//            System.out.println("HTTP trace: " + e.httpTrace());
        }
        return null;
    }
}
